package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddTag;
import com.itheima.ydd.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/ydd_tag")
public class YddTagController {
    @Autowired
    private IYddTagService tagService;
    @Autowired
    private IYddArticleService articleService;

    /**
     * 根据条件分页查询标签
     * @param page
     * @param limit
     * @param tagname
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String tagname,Long page, Long limit ) {
        //分页构造器
        Page pageInfo = new Page(page, limit);
        //条件构造器
        LambdaQueryWrapper<YddTag> lqw = new LambdaQueryWrapper<>();
        //判断是否为空
        lqw.like(StringUtils.isNotEmpty(tagname), YddTag::getTagname, tagname);
        tagService.page(pageInfo, lqw);
        return ResultData.ok("", PageResult.init(pageInfo.getTotal(), page, limit, pageInfo.getRecords()));

    }

    /**
     * 新增标签
     * @param yddTag
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddTag yddTag){

        tagService.save(yddTag);
        return ResultData.ok("",yddTag);
    }

    /**
     * 查询所有标签
     *
     * @return
     */

    @GetMapping("/all")
    public ResultData all(){
        List<YddTag> list = tagService.list();
        return ResultData.ok("",list);
    }

    /**
     * 修改时数据回显
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData getById(Integer id) {
        YddTag tag = tagService.getById(id);
        return ResultData.ok("", tag);
    }

    /**
     * 修改标签
     * @param yddTag
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddTag yddTag){
         tagService.updateById(yddTag);
        return ResultData.ok("修改成功", yddTag);
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete( Integer id){

        //根据id获得tag
        YddTag tag = tagService.getById(id);
        //判断获得的tag是否为默认数据
        if ("1".equals(tag.getDefaultData())){
            return ResultData.error("默认数据无法删除!");
        }
        //条件构造器判断tag标签下是否有文章
        LambdaQueryWrapper<YddArticle> lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddArticle::getTags,tag.getTagname());
        int count = articleService.count(lqw);
        //如果有,不能删除
        if (count>0){
            return ResultData.error("该标签下有关联的文章,无法删除!");
        }
        tagService.removeById(id);
        return ResultData.ok("删除成功", "");
    }
}
